/**
 * Copyright (c) 2018, wuyy All rights reserved.
 * wuyy 专有/保密源代码,未经许可禁止任何人通过任何渠道使用、修改源代码.
 * QQ:16349023
 * 项目名称 : com.wy2.eclipse.tool.ui
 * @version V1.0
 */
package com.wy2.eclipse.tool.ui.menuadapter;

import java.io.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IJavaProject;

import com.ibm.icu.util.StringTokenizer;
import com.wy2.core.tool.StringTools;
import com.wy2.eclipse.tool.lib.common.Constants;

/**
 * GenerateAdaperFactory
 * @author wuyy<16349023@qq.com>
 * @date 2018年1月17日上午11:29:46
 * 
 */
public class GenerateAdapterFactory implements IAdapterFactory {

    /** 
     * @param adaptableObject
     * @param adapterType
     * @return 
     * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object, java.lang.Class)
     */
    @Override
    public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    	IPath projectPath=null;
        if(adaptableObject instanceof IProject) {
            IProject project = (IProject) adaptableObject;   
            projectPath=project.getLocation();
            
        }else if(adaptableObject instanceof IJavaProject) {
        	IJavaProject javaProject=(IJavaProject)adaptableObject;
        	projectPath=javaProject.getProject().getLocation();
        }
        
        if(projectPath!=null) {
        	File generateConfigFile=new File(projectPath.toFile(),Constants.GENERATE_CONFIG_NAME);
            if(adapterType == GenerateProjectAdapter.class && generateConfigFile.exists()) {
                return (T)new GenerateProjectAdapter(adaptableObject);
            }else if(adapterType == CreateConfigProjectAdapter.class && !generateConfigFile.exists()) {
                return (T)new CreateConfigProjectAdapter(adaptableObject);
            }
        }
        
        return null;
    }

    /** 
     * @return 
     * @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
     */
    @Override
    public Class<?>[] getAdapterList() {
        return new Class<?>[] { GenerateProjectAdapter.class,CreateConfigProjectAdapter.class };
    }

}
